#!/usr/bin/env python
'''
Created on Mar 10, 2010

@author: abdelhamid
'''
from PyQt4 import QtCore, QtGui
from sys import *

class PLexical(QtGui.QTabWidget):
    def __init__(self, parent = None):
        super(PLexical, self).__init__(parent)
        self.setupUi(self)
        
    def setupUi(self, TabWidget):
        TabWidget.setObjectName("TabWidget")
        TabWidget.resize(629, 589)
        TabWidget.setTabPosition(QtGui.QTabWidget.North)
        TabWidget.setTabShape(QtGui.QTabWidget.Rounded)
        self.tab = QtGui.QWidget()
        self.tab.setObjectName("tab")
        self.gridLayout_3 = QtGui.QGridLayout(self.tab)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.gridLayout_2 = QtGui.QGridLayout()
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.groupBox = QtGui.QGroupBox(self.tab)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout = QtGui.QGridLayout(self.groupBox)
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtGui.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtGui.QLineEdit(self.groupBox)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.pushButton = QtGui.QPushButton(self.groupBox)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 2, 1, 1)
        self.pushButton_2 = QtGui.QPushButton(self.groupBox)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 1, 2, 1, 1)
        self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 1)
        self.tableWidget_1 = QtGui.QTableWidget(self.tab)
        self.tableWidget_1.setObjectName("tableWidget_1")
        self.tableWidget_1.setColumnCount(2)
        self.tableWidget_1.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_1.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_1.setHorizontalHeaderItem(1, item)
        self.gridLayout_2.addWidget(self.tableWidget_1, 1, 0, 1, 1)
        self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 1)
        TabWidget.addTab(self.tab, "")
        self.tab1 = QtGui.QWidget()
        self.tab1.setObjectName("tab1")
        self.gridLayout_5 = QtGui.QGridLayout(self.tab1)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.gridLayout_4 = QtGui.QGridLayout()
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.textEdit = QtGui.QTextEdit(self.tab1)
        self.textEdit.setObjectName("textEdit")
        self.gridLayout_4.addWidget(self.textEdit, 0, 0, 1, 1)
        self.tableWidget_2 = QtGui.QTableWidget(self.tab1)
        self.tableWidget_2.setObjectName("tableWidget_2")
        self.tableWidget_2.setColumnCount(2)
        self.tableWidget_2.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tableWidget_2.setHorizontalHeaderItem(1, item)
        self.gridLayout_4.addWidget(self.tableWidget_2, 0, 1, 1, 1)
        self.gridLayout_5.addLayout(self.gridLayout_4, 0, 0, 1, 1)
        TabWidget.addTab(self.tab1, "")

        self.retranslateUi(TabWidget)
        TabWidget.setCurrentIndex(0)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.BrowseFile)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), self.LexCheck)
        QtCore.QObject.connect(self.textEdit, QtCore.SIGNAL("textChanged()"), self.LexScan)
        QtCore.QMetaObject.connectSlotsByName(TabWidget)

    def retranslateUi(self, TabWidget):
        TabWidget.setWindowTitle(QtGui.QApplication.translate("TabWidget", "PLexical", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("TabWidget", "Choose File", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("TabWidget", "File", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("TabWidget", "&Browse..", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("TabWidget", "&Check File", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_1.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("TabWidget", "Lexim", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_1.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("TabWidget", "Type", None, QtGui.QApplication.UnicodeUTF8))
        TabWidget.setTabText(TabWidget.indexOf(self.tab), QtGui.QApplication.translate("TabWidget", "File Check", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_2.horizontalHeaderItem(0).setText(QtGui.QApplication.translate("TabWidget", "Lexim", None, QtGui.QApplication.UnicodeUTF8))
        self.tableWidget_2.horizontalHeaderItem(1).setText(QtGui.QApplication.translate("TabWidget", "Type", None, QtGui.QApplication.UnicodeUTF8))
        TabWidget.setTabText(TabWidget.indexOf(self.tab1), QtGui.QApplication.translate("TabWidget", "Lexical Scan", None, QtGui.QApplication.UnicodeUTF8))


    #############################################################
    #########################Scanning############################
    #############################################################
    def scanWhiteSpace(self, scannedText):
        if (scannedText == " ") or (scannedText == "") or (scannedText == "\n") or (scannedText == "\t"):
            return True
        else:
            return False

    def scanLetter(self, scannedText):
        if (scannedText >= 'A' and scannedText <= 'Z') or (scannedText >= 'a' and scannedText <= 'z') or (scannedText == '_'):
            return True
        else:
            return False
            
    def scanDigits(self, scannedText):
        if (scannedText >= '0' and scannedText <= '9'):
            return True
        else:
            return False
        
    def scanOperators(self, scannedText):
        if (scannedText == '+') or (scannedText == '-') or (scannedText =='*') or (scannedText == '='):
            return True
        else:
            return False
    
    def scanReservedwords(self,varTest):
        reservedWords=['if','else','elif','True','False','print','while','for','is','in','None','or','and','equal','not']
        if varTest in reservedWords:    
            return True
        else:
            return False         
  
    def scanCondOperators(self,condTest):
        condOperators=['<','>',"!"]
        if condTest in condOperators:
            return True
        else:
            return False
    
    def scanlogicOperators(self,logicOperators):
        logicOps=['|','&']
        if logicOperators in logicOps:
            return True
        else:
            return False
    
    def scanStringQuotes(self,stringQuotes):
        Quotes=['"',"'"]
        if stringQuotes in Quotes:
            return True
        else:
            return False
    
    def scanExpBrackets(self,expBrackets):
        Brackets=['(',')']
        if expBrackets in Brackets:
            return True
        else:
            return False
    
    def scanListBrackets(self,listBrackets):
        Brackets=['[',']']
        if listBrackets in Brackets:
            return True
        else:
            return False
        
    def BrowseFile(self):
        lexFile = unicode(QtGui.QFileDialog.getOpenFileName(self, 'Open File...', '/media/tmp/Projects/PLexical/src'))
        self.lineEdit.setText(lexFile)
        
                    
    def Scanning(self,stream):
        buffStr=""
        TokensList=[]
        state=0
        for i in stream:
            if state==0:
                ###########################################################
                ###########################################################
                ###########################################################
                if (self.scanLetter(i) == True):
                    buffStr+=i
                    state=1
                    
                elif (self.scanDigits(i) == True):
                    buffStr+=i
                    state=3
                    
                elif (self.scanOperators(i) == True):
                    buffStr=i
                    TokensList.append((buffStr,"Operator"))
                    state=0
                    buffStr=""
                    
                elif (self.scanWhiteSpace(i) == True):
                    state=0
                    buffStr=""
                    continue
                
                elif (self.scanExpBrackets(i) == True):
                    buffStr=i
                    TokensList.append((buffStr,"Expression brackets"))
                    state=0
                    buffStr=""
                
                elif (self.scanStringQuotes(i) == True):
                    buffStr=i
                    TokensList.append((buffStr,"string Quotes"))
                    state=0
                    buffStr=""
                    
                elif (self.scanCondOperators(i) == True):
                    buffStr=i
                    TokensList.append((buffStr,"conditional operator"))
                    state=0
                    buffStr=""
                
                elif (self.scanlogicOperators(i) == True):
                    buffStr=i
                    TokensList.append((buffStr,"logic operator"))
                    state=0
                    buffStr=""
                    
                elif (self.scanListBrackets(i) == True):
                    buffStr=i
                    TokensList.append((buffStr,"List Brackets"))
                    state=0
                    buffStr=""
                
                elif (i == ":"):
                    buffStr=i
                    TokensList.append((buffStr,"start statement body"))
                    state=0
                    buffStr=""
                    
                else:
                    buffStr=i
                    TokensList.append((buffStr,"Invalid Entery"))
                    state=0
                    buffStr=""
            
                    
            
            elif state==1:
                if (self.scanDigits(i) == True) or (self.scanLetter(i) == True):
                    buffStr+=i
                    
                else:
                    if self.scanReservedwords(buffStr) == True:
                        TokensList.append((buffStr,"Reserved"))
                        getInv=i
                        buffStr=""
                        ###########################################################
                        ###########################################################
                        ###########################################################
                        if (self.scanOperators(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"Operator"))
                            buffStr=""
                            state=0
                        
                        elif (self.scanWhiteSpace(getInv) == True):
                            state=0
                            buffStr=""
                            continue

                        elif (self.scanExpBrackets(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"Expression brackets"))
                            state=0
                            buffStr=""
                        
                        elif (self.scanStringQuotes(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"string Quotes"))
                            state=0
                            buffStr=""
                            
                        elif (self.scanCondOperators(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"conditional operator"))
                            state=0
                            buffStr=""
                        
                        elif (self.scanlogicOperators(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"logic operator"))
                            state=0
                            buffStr=""
                            
                        elif (self.scanListBrackets(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"List Brackets"))
                            state=0
                            buffStr=""
                        
                        elif (getInv == ":"):
                            buffStr=getInv
                            TokensList.append((buffStr,"start statement body"))
                            state=0
                            buffStr=""

                        else:
                            buffStr=getInv
                            TokensList.append((buffStr,"Invalid Entery"))
                            state=0
                            buffStr=""
                                                
                    else:    
                        TokensList.append((buffStr,"Variable"))
                        getInv=i
                        buffStr=""
                        ###########################################################
                        ###########################################################
                        ###########################################################
                        if (self.scanOperators(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"Operator"))
                            buffStr=""
                            state=0
                        
                        elif (self.scanWhiteSpace(getInv) == True):
                            state=0
                            buffStr=""
                            continue

                        elif (self.scanExpBrackets(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"Expression brackets"))
                            state=0
                            buffStr=""
                        
                        elif (self.scanStringQuotes(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"string Quotes"))
                            state=0
                            buffStr=""
                            
                        elif (self.scanCondOperators(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"conditional operator"))
                            state=0
                            buffStr=""
                        
                        elif (self.scanlogicOperators(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"logic operator"))
                            state=0
                            buffStr=""
                            
                        elif (self.scanListBrackets(getInv) == True):
                            buffStr=getInv
                            TokensList.append((buffStr,"List Brackets"))
                            state=0
                            buffStr=""
                        
                        elif (getInv == ":"):
                            buffStr=getInv
                            TokensList.append((buffStr,"start statement body"))
                            state=0
                            buffStr=""

                        else:
                            buffStr=getInv
                            TokensList.append((buffStr,"Invalid Entery"))
                            state=0
                            buffStr=""
                        state=0
            
            
            
            elif state==3:
                if (self.scanDigits(i) == True):
                    buffStr+=i
                    
                else:
                    TokensList.append((buffStr,"Number"))
                    getInv=i
                    buffStr=""
                    ###########################################################
                    ###########################################################
                    ###########################################################
                    if (self.scanLetter(getInv) == True):
                        buffStr+=getInv
                        state=1
                        continue

                    if (self.scanOperators(getInv) == True):
                        buffStr=getInv
                        TokensList.append((buffStr,"Operator"))
                        buffStr=""
                        state=0
                    
                    elif (self.scanWhiteSpace(getInv) == True):
                        state=0
                        buffStr=""
                        continue

                    elif (self.scanExpBrackets(getInv) == True):
                        buffStr=getInv
                        TokensList.append((buffStr,"Expression brackets"))
                        state=0
                        buffStr=""
                    
                    elif (self.scanStringQuotes(getInv) == True):
                        buffStr=getInv
                        TokensList.append((buffStr,"string Quotes"))
                        state=0
                        buffStr=""
                        
                    elif (self.scanCondOperators(getInv) == True):
                        buffStr=getInv
                        TokensList.append((buffStr,"conditional operator"))
                        state=0
                        buffStr=""
                    
                    elif (self.scanlogicOperators(getInv) == True):
                        buffStr=getInv
                        TokensList.append((buffStr,"logic operator"))
                        state=0
                        buffStr=""
                        
                    elif (self.scanListBrackets(getInv) == True):
                        buffStr=getInv
                        TokensList.append((buffStr,"List Brackets"))
                        state=0
                        buffStr=""
                    
                    elif (getInv == ":"):
                        buffStr=getInv
                        TokensList.append((buffStr,"start statement body"))
                        state=0
                        buffStr=""                    
                    else:
                        buffStr=getInv
                        TokensList.append((buffStr,"Invalid Entery"))
                        state=0
                        buffStr=""
                    state=0
        
        return TokensList
        

    def LexCheck(self):
        plainFile=unicode(self.lineEdit.text())
        openedFile=open(plainFile,'r+')
        streamText=openedFile.read()
        TokensList = self.Scanning(streamText)                  
        self.tableWidget_1.setRowCount(len(TokensList))        
        for i in range(len(TokensList)):
            item = QtGui.QTableWidgetItem(TokensList[i][0])
            item2 = QtGui.QTableWidgetItem(TokensList[i][1])
            self.tableWidget_1.setItem(i,0,item)       
            self.tableWidget_1.setItem(i,1,item2)

        
    def LexScan(self):
        streamText=self.textEdit.toPlainText()
        TokensList = self.Scanning(streamText)                 
        self.tableWidget_2.setRowCount(len(TokensList))        
        for i in range(len(TokensList)):
            item = QtGui.QTableWidgetItem(TokensList[i][0])
            item2 = QtGui.QTableWidgetItem(TokensList[i][1])
            self.tableWidget_2.setItem(i,0,item)       
            self.tableWidget_2.setItem(i,1,item2)


if __name__ == '__main__':
    app = QtGui.QApplication(argv)
    abstract_window = PLexical()
    abstract_window.show()
    app.exec_()
